/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package banksystem;

import javax.swing.JOptionPane;

/**
 *
 * @author conra_000
 */
public class ModifyUserFrame extends javax.swing.JFrame {
	Controller mController = null;
	AddressNormalizer mAddress = null;
	boolean mIsEdit = false;
	
	/**
	 * Creates new form ModifyUserFrame
	 */
	public ModifyUserFrame(Controller controller) {
		initComponents();
		
		this.windowTitleLabel.setText("Enter User Info");
		ClearErrors();
		
		mController = controller;
	}
	
	public ModifyUserFrame(Controller controller, String fName, String lName, String address, String phoneNumber) {
		initComponents();
		
		this.fNameTextField.setText(fName);
		this.fNameTextField.setEditable(false);
		this.lNameTextField.setText(lName);
		this.lNameTextField.setEditable(false);
		this.addressTextField.setText(address);
		this.phoneTextField.setText(phoneNumber);
		this.windowTitleLabel.setText("Edit User Data");
		ClearErrors();
		
		mIsEdit = true;
		mController = controller;
	}
	
	

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        fNameTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        lNameTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        addressTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        phoneTextField = new javax.swing.JTextField();
        windowTitleLabel = new javax.swing.JLabel();
        SubmitButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        fNameError = new javax.swing.JLabel();
        lNameError = new javax.swing.JLabel();
        addressError = new javax.swing.JLabel();
        phoneError = new javax.swing.JLabel();
        addressValidateButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("First Name");

        jLabel2.setText("Last Name");

        jLabel3.setText("Address");

        jLabel4.setText("Phone Number");

        windowTitleLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        windowTitleLabel.setText("Please...");

        SubmitButton.setText("Submit");
        SubmitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SubmitButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        fNameError.setForeground(new java.awt.Color(255, 51, 51));
        fNameError.setText("fNameError");

        lNameError.setForeground(new java.awt.Color(255, 51, 51));
        lNameError.setText("lNameError");

        addressError.setForeground(new java.awt.Color(255, 51, 51));
        addressError.setText("addressError");

        phoneError.setForeground(new java.awt.Color(255, 51, 51));
        phoneError.setText("phoneError");

        addressValidateButton.setText("Check Address");
        addressValidateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addressValidateButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(77, 77, 77)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addressValidateButton)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(windowTitleLabel)
                        .addComponent(fNameTextField)
                        .addComponent(lNameTextField)
                        .addComponent(phoneTextField)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(23, 23, 23)
                            .addComponent(SubmitButton)
                            .addGap(18, 18, 18)
                            .addComponent(CancelButton))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(phoneError, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addGap(18, 18, 18)
                            .addComponent(addressError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lNameError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(fNameError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(addressTextField)))
                .addContainerGap(71, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(windowTitleLabel)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(fNameError))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lNameError))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(addressError))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addressTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(addressValidateButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(phoneError))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(phoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SubmitButton)
                    .addComponent(CancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void SubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitButtonActionPerformed
		String fName = "";
		String lName = "";
		String address = "";
		String phoneNumber = "";
		boolean error = false;
		ClearErrors();
		
		try {
			fName = getName(this.fNameTextField);
        } 
		catch (Exception e) {
			String errorMessage;
            if(e.getClass() == NullPointerException.class) {
				errorMessage = "Error: Please Enter First Name";
			} else if(e.getClass() == IllegalArgumentException.class) {
				errorMessage = e.getMessage();
			} else {
				errorMessage = "Error: Unknown";
			}
			this.fNameError.setText(errorMessage);
            error = true;
        }
		
		try {
			lName = getName(this.lNameTextField);
        } 
		catch (Exception e) {
			String errorMessage;
            if(e.getClass() == IllegalArgumentException.class) {
				errorMessage = e.getMessage();
			} else {
				errorMessage = "Error: Unknown";
			}
			this.lNameError.setText(errorMessage);
            error = true;
        }
		
		try {
			address = getAddress(this.addressTextField);
        } 
		catch (Exception e) {
			String errorMessage;
            if(e.getClass() == IllegalArgumentException.class) {
				errorMessage = e.getMessage();
			} else {
				errorMessage = "Error: Unknown";
			}
			this.addressError.setText(errorMessage);
            error = true;
        }
		
		try {
			phoneNumber = getPhone(this.phoneTextField);
        } 
		catch (Exception e) {
			String errorMessage;
            if(e.getClass() == IllegalArgumentException.class) {
				errorMessage = e.getMessage();
			} else {
				errorMessage = "Error: Unknown";
			}
			this.phoneError.setText(errorMessage);
            error = true;
        }
		
		if(error == false) {
			mController.ModifyUserInfo(fName, lName, address, phoneNumber, mIsEdit);
		} else {
			return;
		}
	}//GEN-LAST:event_SubmitButtonActionPerformed

	private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
		mController.CancelUserModify(mIsEdit);
	}//GEN-LAST:event_CancelButtonActionPerformed

	private void addressValidateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addressValidateButtonActionPerformed
		String addressInput;
		String normalizedAddress = "";
		this.addressError.setText(null);
		boolean error = false;
		
		try {
			addressInput = getAddress(this.addressTextField);
			
			AddressNormalizer addressNormalizer = new AddressNormalizer(addressInput);
			addressNormalizer.GeoLookup();
			normalizedAddress = addressNormalizer.getNormalized();
        } 
		catch (Exception e) {
			String errorMessage;
            if(e.getClass() == IllegalArgumentException.class) {
				errorMessage = e.getMessage();
			} else {
				errorMessage = "Error: Unknown";
			}
			this.addressError.setText(errorMessage);
            error = true;
        }

		if(error == false) {
			this.addressTextField.setText(normalizedAddress);
		} else {
			return;
		}
	}//GEN-LAST:event_addressValidateButtonActionPerformed

	private String getAddress(javax.swing.JTextField addressField) 
			throws NullPointerException, IllegalArgumentException
	{
		String addressInputString = addressField.getText();
		
		if("".equals(addressInputString)) {
			throw new IllegalArgumentException("Please provide Address");
		}
		
		return addressInputString;
	}
	
	private String getName(javax.swing.JTextField nameField) 
			throws NullPointerException, IllegalArgumentException
	{
		String nameString = nameField.getText();
		if("".equals(nameString)) {
			throw new IllegalArgumentException("Please provide Name");
		}
		int nameLen = nameString.length();
		if( nameLen < 3 || nameLen > 15) {
			throw new IllegalArgumentException("Error: Must be between 3 and 15 chars");
		}
	
		return nameString;
	}
	
	private String getPhone(javax.swing.JTextField phoneField) 
			throws NullPointerException, IllegalArgumentException
	{
		String phoneString = phoneField.getText();
		if("".equals(phoneString)) {
			throw new IllegalArgumentException("Please provide a Number");
		}
		if(phoneString.matches("^[+]?[01]?[- .]?(\\([2-9]\\d{2}\\)|[2-9]\\d{2})[- .]?\\d{3}[- .]?\\d{4}$")) {
			return phoneString;
		} else {
			throw new IllegalArgumentException("Error: Invalid number");
		}
	}
	
	private void ClearErrors() {
		this.fNameError.setText(null);
		this.lNameError.setText(null);
		this.addressError.setText(null);
		this.phoneError.setText(null);
	}
	
	/**
	 * @param args the command line arguments
	 */
	
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton SubmitButton;
    private javax.swing.JLabel addressError;
    private javax.swing.JTextField addressTextField;
    private javax.swing.JButton addressValidateButton;
    private javax.swing.JLabel fNameError;
    private javax.swing.JTextField fNameTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lNameError;
    private javax.swing.JTextField lNameTextField;
    private javax.swing.JLabel phoneError;
    private javax.swing.JTextField phoneTextField;
    private javax.swing.JLabel windowTitleLabel;
    // End of variables declaration//GEN-END:variables
}
